import { Injectable } from '@nestjs/common'
@Injectable()
export class DateService {
  get nowDayOfWeek() {
    return this.dayDate.getDay()
  }

  get nowDay() {
    return this.dayDate.getDate()
  }

  get nowMonth() {
    return this.dayDate.getMonth()
  }

  get nowYear() {
    return this.dayDate.getFullYear()
  }

  get lastMonth() {
    var lastMonthDate = new Date() //上月日期
    lastMonthDate.setDate(1)
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
    return lastMonthDate.getMonth()
  }

  get lastYear() {
    var lastMonthDate = new Date() //上月日期
    lastMonthDate.setDate(1)
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1)
    return lastMonthDate.getFullYear()
  }

  // 今日
  get dayDate() {
    const nowDate = new Date()
    nowDate.setHours(0)
    nowDate.setMinutes(0)
    nowDate.setSeconds(0)
    return nowDate
  }
  // 本月
  get monthDate() {
    return new Date(this.nowYear, this.nowMonth, 1)
  }
  // 本周
  get weekDate() {
    return new Date(this.nowYear, this.nowMonth, this.nowDay - this.nowDayOfWeek)
  }

  // 本季度
  get jiduDate() {
    return new Date(this.nowYear, this.getQuarterStartMonth(), 1)
  }

  // 上月时期
  get lastMonthDate() {
    return {
      start: new Date(this.nowYear, this.lastMonth, 1),
      end: new Date(this.nowYear, this.lastMonth, this.getMonthDays(this.lastMonth))
    }
  }

  private getQuarterStartMonth() {
    let quarterStartMonth = 0
    if (this.nowMonth < 3) {
      quarterStartMonth = 0
    }
    if (2 < this.nowMonth && this.nowMonth < 6) {
      quarterStartMonth = 3
    }
    if (5 < this.nowMonth && this.nowMonth < 9) {
      quarterStartMonth = 6
    }
    if (this.nowMonth > 8) {
      quarterStartMonth = 9
    }
    return quarterStartMonth
  }

  private getMonthDays(myMonth) {
    const monthStartDate = new Date(this.nowYear, myMonth, 1).getTime()
    const monthEndDate = new Date(this.nowYear, myMonth + 1, 1).getTime()
    const days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
    return days
  }
}
